// 这里是写vuex的mutations
import Vue from 'vue'
export default {
    SET_OPENID: (state, openId) => {
        localStorage.setItem("openId", openId)
        state.openId = openId
    },
    SET_USERID: (state, userId) => {
        localStorage.setItem("userId", userId)
        state.userId = userId
    },
    SET_SCHOOLID: (state, schoolId) => {
        localStorage.setItem("schoolId", schoolId)
        state.schoolId = schoolId
    },
    SET_PHONE: (state, username) => {
        localStorage.setItem("phone", username)
        state.phone = username
    },
    SET_AVATAR: (state, avatar) => {
        localStorage.setItem("avatar", avatar)
        state.avatar = avatar
    },
    SET_PASSWORD: (state, password) => {
        localStorage.setItem("password", password)
        state.password = password
    },
    SET_SCHOOLCODE: (state, schoolCode) => {
        localStorage.setItem("schoolCode", schoolCode)
        state.schoolCode = schoolCode
    },
    SET_IMLOGINSTATUS: (state, imIsLogin) => {
        state.IMISLOGIN = imIsLogin
    },
    UpdataCarList(state, message) {
        state.carts = message
    },
    updateCartShopCount(state, {
        dianId,
        shopIndex,
        playload
    }) {
        state.carts[dianId].shops[shopIndex].count = playload
    },
    //删除商品
    delShopCart(state,{dianId,       
         shopIndex
        }){
       state.carts[dianId].shops.splice(0,1)
       for (let i=0;i<state.carts.length;i++){
        if(state.carts[i].shops.length==0){
            state.carts.splice(i,1)
           }
       }
       
    },
    updateCartShopSelect(state, { dianId, shopIndex, playload }) {
        state.carts[dianId].shops[shopIndex].select = playload
    },
    //获取购物车
    UpCarts(state, res) {
        // alert('进入mutation');
        localStorage.setItem("order",JSON.stringify(res)) //本地进行存储
        state.carts = res
        state.order=res
    },
    DO_LOGIN_IM: (state, avatar) => {
        const encryptUsername = "hukai123"
        Vue.prototype.$imConn.listen({
            onOpened: function (message) {
                 console.log('%c [opened] 连接已成功建立','color:green')
            },
            onTextMessage: function (message) {
                //在此接收和处理消息，根据message.type区分消息来源，私聊或群组或聊天室
                console.log(message)
                console.log(message.type)
                console.log('text')
            },
            onEmojiMessage: function (message) {
                console.log('Emoji')
                let data = message.data
                for (let i=0,l=data.length;i<l;i++) {
                    console.log(data[i])
                }
            },
            onPictureMessage: function (message) {
                console.log('Picture')
                let options = {url: message.url}
                options.onFileDownloadComplete = function(){
                    //图片下载成功
                    console.log('Image download complete!')
                }
                options.onFileDownloadError = function(){
                    //图片下载失败
                    console.log('Image download failed!')
                }
                Vue.prototype.$WebIM.utils.download.call(Vue.prototype.$imConn,options)
            },
            onRoster: function (message) {
                console.log('Roster')
                console.log(message)
            }
        })
        Vue.prototype.$imConn.open({
            apiUrl: Vue.prototype.$WebIM.config.apiURL,
            user: 'hukai123',
            pwd: '123456',
            appKey: Vue.prototype.$WebIM.config.appKey,
            success: function (token) {
                let tokenStr = token.access_token;
                Vue.prototype.$WebIM.utils.setCookie('webim'+encryptUsername,tokenStr,1)
            },
            error: function() {
                
            }
        })

    },

}